from osv import fields
from osv import osv
import wizard
import netsvc
import pooler
import time
from osv.orm import browse_record
import pooler

#Project to Contracts
class wiz_project_contract_open(wizard.interface):
    def _open_contract(self, cr, uid, data, context):
        cr.execute("Select\
                            contract_id \
                    from \
                            sale_order \
                    where \
                        project_e_id in (%s) or project_m_id in (%s)"  % (','.join(map(str,data['ids'])),','.join(map(str,data['ids']))))
        ctc_ids = []
        for ctc_id in cr.fetchall():
            ctc_ids.append(ctc_id)
        ctc_ids= ','.join(map(str,ctc_ids))
        value = {
            'domain': "[('id', 'in',["+ctc_ids+"])]",
            'name': 'Contract',
            'view_type': 'form',
            'view_mode': 'tree,form',
            'res_model': 'kdvn.contract.client',
            'view_id': False,
            'type': 'ir.actions.act_window'
        }
        return value

    states = {
        'init' : {
            'actions' : [],
            'result' : {'type':'action', 'action':_open_contract, 'state':'end'}
        }
    }
wiz_project_contract_open('project.contract.open')

#Project to Purchase
class wiz_project_puo_open(wizard.interface):
    def _open_purchase_order(self, cr, uid, data, context):
        cr.execute("Select\
                        po.id\
                    from \
                        purchase_order po \
                    left join\
                        purchase_typeoforder pt on typeoforder=pt.id\
                    where typeoforder not in (3,5) and project_id in (%s)" % ','.join(map(str,data['ids'])))
        po_ids = []
        str_context=""
        if data['ids']:
            str_context={'project_id':data['ids'][0],'prj_context_id':data['ids'][0]}
        
        for po_id in cr.fetchall():
            po_ids.append(po_id)
        po_ids= ','.join(map(str,po_ids))
        value = {
            'domain': "[('id', 'in',["+po_ids+"])]",
            'name': 'Purchase',
            'view_type': 'form',
            'view_mode': 'tree,form',
            'context':str_context,
            'res_model': 'purchase.order',
            'view_id': False,
            'type': 'ir.actions.act_window'
        }
        return value
    states = {
        'init' : {
            'actions' : [],
            'result' : {'type':'action', 'action':_open_purchase_order, 'state':'end'}
        }
    }
wiz_project_puo_open('project.po.open')

#Project to Other Expense
class wiz_project_expense_open(wizard.interface):
    def _open_expense(self, cr, uid, data, context):

        cr.execute("Select\
                        id\
                    from \
                        kderp_other_expense \
                    where project_id in (%s)" % ','.join(map(str,data['ids'])))
        expenses_ids = []
        for expense_id in cr.fetchall():
            expenses_ids.append(expense_id)
        expenses_ids= ','.join(map(str,expenses_ids))
        prj_context_id =False
        if (len(data['ids'])==1):
            prj_context_id =data['ids'][0] 
        value = {
            'domain': "[('id', 'in',["+expenses_ids+"])]",
            'name': 'Other Expense',
            'view_type': 'form',
            'view_mode': 'tree,form',
            'res_model': 'kderp.other.expense',
            'type': 'ir.actions.act_window',
            'context':{'prj_context_id':prj_context_id}
        }
        return value
    states = {
        'init' : {
            'actions' : [],
            'result' : {'type':'action', 'action':_open_expense, 'state':'end'}
        }
    }
wiz_project_expense_open('project.expense.open')

#Project to Advance Payment
class wiz_project_adv_open(wizard.interface):
    def _open_adv(self, cr, uid, data, context):
        cr.execute("Select\
                        id\
                    from \
                        kderp_advance_payment \
                    where project_id in (%s)" % ','.join(map(str,data['ids'])))
        adv_ids = []
        for adv_id in cr.fetchall():
            adv_ids.append(adv_id)
        adv_ids = ','.join(map(str,adv_ids))
        prj_context_id =False
        if (len(data['ids'])==1):
            prj_context_id =data['ids'][0]
            
        value = {
            'domain': "[('id', 'in',["+adv_ids+"])]",
            'name': 'Advance Payment',
            'view_type': 'form',
            'view_mode': 'tree,form',
            'res_model': 'kderp.advance.payment',
            'type': 'ir.actions.act_window',
            'context':{'prj_context_id':prj_context_id}
        }
        return value
    states = {
        'init' : {
            'actions' : [],
            'result' : {'type':'action', 'action':_open_adv, 'state':'end'}
        }
    }
wiz_project_adv_open('project.adv.open')


class wiz_contract_po_open(wizard.interface):
    def _open_purchase_order(self, cr, uid, data, context):
        cr.execute("Select\
                        po.id\
                    from \
                        purchase_order po \
                    where project_id in\
                        (Select \
                            project_id\
                        from kdvn_project_contract_rel where contract_id in (%s))"  % ','.join(map(str,data['ids'])))
        po_ids = []
        for po_id in cr.fetchall():
            po_ids.append(po_id)
        po_ids= ','.join(map(str,po_ids))
        value = {
            'domain': "[('id', 'in', ["+po_ids+"])]",
            'name': 'Purchase Orders',
            'view_type': 'form',
            'view_mode': 'tree,form',
            'res_model': 'purchase.order',
            'view_id': False,
            'type': 'ir.actions.act_window',
            'context':"{'typeoforder':1}",
        }
        return value

    states = {
        'init' : {
            'actions' : [],
            'result' : {'type':'action', 'action':_open_purchase_order, 'state':'end'}
        }
    }
wiz_contract_po_open('contract.po.open')

class wiz_project_payment_to_supplier_open(wizard.interface):
    def _open_rop_order(self, cr, uid, data, context):
        cr.execute("Select distinct\
                        krop.id\
                    from \
                        kdvn_request_of_payment krop\
                    where krop.project_id>0 and order_id>0 and krop.project_id in (%s)"  % ','.join(map(str,data['ids'])))
        
        rop_ids = []
        for rop_id in cr.fetchall():
            rop_ids.append(rop_id)
        rop_ids= ','.join(map(str,rop_ids))
        
        value = {
            'domain': "[('id', 'in', ["+rop_ids+"])]",
            'name': 'Payment to Supplier',
            'view_type': 'form',
            'view_mode': 'tree,form',
            'res_model': 'kdvn.request.of.payment',
            'view_id': False,
            'type': 'ir.actions.act_window'
        }
        return value

    states = {
        'init' : {
            'actions' : [],
            'result' : {'type':'action', 'action':_open_rop_order, 'state':'end'}
        }
    }
wiz_project_payment_to_supplier_open('project.rop.open')

#class wiz_contract_payment_to_supplier_open(wizard.interface):
#    def _open_contract_rop_order(self, cr, uid, data, context):
#        cr.execute("Select distinct\
#                        rop_id\
#                    from \
#                        kdvn_request_of_payment_detail kropd \
#                    left join\
#                        kdvn_request_of_payment krop on rop_id = krop.id\
#                    where kropd.project_id>0 and order_id>0 and kropd.project_id in\
#                        (Select \
#                            project_e_id\
#                        from sale_order where contract_id in (%s)\
#                        union \
#                        Select \
#                            project_m_id\
#                        from sale_order where contract_id in (%s))"  % (','.join(map(str,data['ids'])),','.join(map(str,data['ids']))))
#        po_ids = []
#        for po_id in cr.fetchall():
#            po_ids.append(po_id)
#        po_ids= ','.join(map(str,po_ids))
#        value = {
#            'domain': "[('id', 'in', ["+po_ids+"])]",
#            'name': 'Payment to Supplier',
#            'view_type': 'form',
#            'view_mode': 'tree,form',
#            'res_model': 'kdvn.request.of.payment',
#            'view_id': False,
#            'type': 'ir.actions.act_window'
#        }
#        return value
#
#    states = {
#        'init' : {
#            'actions' : [],
#            'result' : {'type':'action', 'action':_open_contract_rop_order, 'state':'end'}
#        }
#    }
#wiz_contract_payment_to_supplier_open('contract.rop.open')

class wiz_po_payment_to_supplier_open(wizard.interface):
    def _open_po_pts(self, cr, uid, data, context):
        cr.execute("Select\
                        id\
                    from \
                        kdvn_request_of_payment \
                    where order_id in (%s)"  % ','.join(map(str,data['ids'])))
        po_ids = []
        for po_id in cr.fetchall():
            po_ids.append(po_id)
        po_ids= ','.join(map(str,po_ids))
        value = {
            'domain': "[('id', 'in', ["+po_ids+"])]",
            'name': 'Payment to Supplier',
            'view_type': 'form',
            'view_mode': 'tree,form',
            'res_model': 'kdvn.request.of.payment',
            'view_id': False,
            'type': 'ir.actions.act_window'
        }
        return value

    states = {
        'init' : {
            'actions' : [],
            'result' : {'type':'action', 'action':_open_po_pts, 'state':'end'}
        }
    }
wiz_po_payment_to_supplier_open('po.rop.open')

class wiz_project_budget_status(wizard.interface):
    def _open_project_budget_status(self, cr, uid, data, context):
        cr.execute("Select\
                        id\
                    from \
                        kdvn_report_project_budget_status \
                    where project_id in (%s)"  % ','.join(map(str,data['ids'])))
        po_ids = []
        for po_id in cr.fetchall():
            po_ids.append(po_id)
        po_ids= ','.join(map(str,po_ids))
        value = {
            'domain': "[('id', 'in', ["+po_ids+"])]",
            'name': 'Project - Budget Status',
            'view_type': 'form',
            'view_mode': 'tree,form',
            'res_model': 'kdvn.report.project.budget.status',
            'view_id': False,
            'type': 'ir.actions.act_window'
        }
        return value

    states = {
        'init' : {
            'actions' : [],
            'result' : {'type':'action', 'action':_open_project_budget_status, 'state':'end'}
        }
    }
wiz_project_budget_status('project.budget.status.open')